package com.qfedu.vhr.controller.chat;

import com.qfedu.vhr.common.Chat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

/**
 * @author baize
 * @date 2023/1/12
 * @site www.qfedu.com
 */
@Controller
public class WsController {

    //这个是消息发送模板
    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;

    //将来前端发消息，就发送到这个地址上
    @MessageMapping("/myws/chat")
    public void chat(Chat chat, Principal principal) {
        //获取当前登录的用户名
        String name = principal.getName();
        chat.setFrom(name);
        simpMessagingTemplate.convertAndSendToUser(chat.getTo(), "/queue/chat", chat);
    }
}
